package com.deer.wms.system.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.service.SuperServiceImpl;
import com.deer.wms.system.manage.dao.CustomerMapper;
import com.deer.wms.system.manage.model.customer.Customer;
import com.deer.wms.system.manage.model.customer.CustomerCriteria;
import com.deer.wms.system.manage.model.customer.CustomerDto;
import com.deer.wms.system.manage.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * @author guo
 * @since 2019/11/13
 */
@Service
@Transactional
public class CustomerServiceImpl extends SuperServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private CustomerService customerService;

    @Override
    public List<CustomerDto> findList(CustomerCriteria criteria){
        return customerMapper.findList(criteria);
    }

    @Override
    public List<CustomerDto> findByCustomerCode(String customerCode){
        return customerMapper.findByCustomerCode(customerCode);
    }

    @Override
    public List<CustomerDto> findByCustomerName(String customerName){
        return customerMapper.findByCustomerName(customerName);
    }

    @Override
    public Result createCustomer(CustomerDto customer, CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }

        /**
         * 新建客户
         */
        List<CustomerDto> list = customerService.findByCustomerCode(customer.getCustomerCode());
        List<CustomerDto> list1 = customerService.findByCustomerName(customer.getCustomerName());
        if (list.size() == 0 && list1.size() == 0) {
            save(customer);
            return ResultGenerator.genSuccessResult();
        } else if (list.size() == 0 && list1.size() != 0) {
            throw new ServiceException(CommonCode.CUSTOMER_EXISTS, "此客户名称已存在");
        } else{
            throw new ServiceException(CommonCode.CUSTOMER_CODE_EXISTS, "此客户编码已存在");

        }
    }

    @Override
    public Customer findBy(String field, String value) {
        QueryWrapper<Customer> qw = new QueryWrapper<>();
        return getOne(qw.eq(field, value));
    }
}
